<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/13
 * Time: 18:02
 */
namespace app\shop;


use app\common\Common;
use app\data\ShopData;
use app\order\Order;
use think\Db;

class ShopGoods extends Base
{
    /**
     * 商品名字长度限制
     * @var array
     */
    private $goodsName = [1, 150];

    /**
     * 排序字段最大值
     * @var int
     */
    private $sortMax = 65535;

    /**
     * 砍价商品排序
     * @var array
     */
    private $bargainSort = ['create_time'];

    /**
     * 排序方法
     * @var array
     */
    private $sortType = ['desc', 'asc'];

    /**
     * 商品排序
     * @var array
     */
    private $sortGoods = ['create_time'];
    /**
     * 分享长度设置
     * @var array
     */
    private $titleMax = [1, 20];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 添加商品基本校验
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param $goods_group
     * @param $is_shelves
     * @param $goods_stock
     * @param $goods_hot
     * @param $goods_new
     * @param $goods_freight
     * @param $goods_img
     * @param $goods_info
     * @param $is_proxy
     * @return array
     */
    private function check($shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id, $goods_group,
                           $is_shelves, $goods_stock, $goods_hot, $goods_new, $goods_freight, $goods_img, $goods_info
        , $is_proxy)
    {
        if (Common::checkTableFields('drh_shop', 'id', $shop_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该商户'];
        }
        if (Common::checkTableFieldsValue('drh_goods_type', 'shop_id', 'id', $goods_type_id, $shop_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该分类'];
        }
        $check = ['商品分类' => ["{$this->goodsName[0]},{$this->goodsName[1]}}", $goods_name],];
        $res = Common::checkLength($check);
        if ($res['code'] == $this->f_code) {
            return $res;
        }
        $arr = [$goods_price, $give_integral, $goods_stock, $goods_hot, $goods_new, $goods_freight];

        foreach ($arr as $k => $v) {
            if (!Common::checkNumber($v, 0)) {
                return ['code' => $this->f_code, 'msg' => '输入数字不合法'];
            }
        }
        if ($goods_group != 0 && $goods_group != 1 && $goods_group != 2) {
            return ['code' => $this->f_code, 'msg' => '商品分组错误'];
        }
        if ($is_shelves != 0 && $is_shelves != 1 && $is_shelves != 2) {
            return ['code' => $this->f_code, 'msg' => '是否上架填写错误'];
        }
        if ($goods_new > $this->sortMax || $goods_hot > $this->sortMax) {
            return ['code' => $this->f_code, 'msg' => '超过最大排序值'];
        }
        if ($is_proxy != 0 && $is_proxy != 1) {
            return ['code' => $this->f_code, 'msg' => '代理关系错误'];
        }
        if (count($goods_img) > 10 || count($goods_info) > 10) {
            return ['code' => $this->f_code, 'msg' => '商品图片或详情图片最多不超过10张'];
        }
        return ['code' => $this->t_code];
    }

    /**
     * 添加商品
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param int $goods_group
     * @param int $is_shelves
     * @param $goods_stock
     * @param int $goods_hot
     * @param int $goods_new
     * @param int $goods_freight
     * @param array $goods_attr_value
     * @param array $goods_info
     * @param array $goods_img
     * @param  $is_proxy
     * @return \think\response\Json
     */
    public function addGoods($shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                             $goods_group = 0, $is_shelves = 1, $goods_stock, $goods_freight, $goods_hot = 1000, $goods_new = 1000,
                             array $goods_attr_value = [], array $goods_info = [], array $goods_img = [], $is_proxy)
    {
        $price = $goods_price;
        if ($goods_price == 0) {
            $price = 1;
        }
        $res = $this->check($shop_id, $goods_name, $price, $give_integral, $goods_type_id,
            $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new, $goods_freight, $goods_img, $goods_info, $is_proxy);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }

        $shop = new ShopData();
        return json($shop->addGoods($shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
            $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
            json_encode($goods_attr_value), json_encode($goods_info), json_encode($goods_img), $goods_freight, $is_proxy));
    }

    /**
     * 修改商品属性
     * @param $id
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param int $goods_group
     * @param int $is_shelves
     * @param $goods_stock
     * @param int $goods_hot
     * @param int $goods_new
     * @param int $goods_freight
     * @param array $goods_attr_value
     * @param array $goods_info
     * @param array $goods_img
     * @param  $is_proxy
     * @return \think\response\Json
     */
    public
    function edit($id, $shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                  $goods_group = 0, $is_shelves = 1, $goods_stock, $goods_hot = 1000, $goods_new = 1000,
                  array $goods_attr_value = [], array $goods_info = [], array $goods_img = [], $goods_freight, $is_proxy)
    {
        $price = $goods_price;
        if ($goods_price == 0) {
            $price = 1;
        }
        $res = $this->check($shop_id, $goods_name, $price, $give_integral, $goods_type_id,
            $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new, $goods_freight, $goods_img, $goods_info, $is_proxy);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFieldsValue('drh_goods', 'shop_id', 'id', $id, $shop_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商品']);
        }
        $shop = new ShopData();
        Db::startTrans();
        try {
            $res = $shop->editGoods($id, $shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                $goods_attr_value, $goods_info, $goods_img, $goods_freight, $is_proxy);
            if ($is_proxy == 0 || $is_shelves >= 2) {//不代理
                $this->cancelGoodsProxy($id);//删除所有的代理商品
            } else {//修改代理商品属性
                $shop->editProxyGoods($id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                    $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                    json_encode($goods_attr_value), json_encode($goods_info), json_encode($goods_img), $goods_freight, $is_proxy);
            }
            Db::commit();
            return json($res);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 删除商品
     * @param $id
     * @param $shop_id
     * @param $is_shelves
     * @return \think\response\Json
     */
    public
    function delGoods($id, $shop_id, $is_shelves)
    {
        if (Common::checkTableFieldsValue('drh_goods', 'shop_id', 'id', $id, $shop_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商品']);
        }
        $order = new Order();
        if ($is_shelves != 0 && $is_shelves != 1 && $is_shelves != 2) {
            return json(['code' => $this->f_code, 'msg' => '操作不合法']);
        }
        $count = $order->checkTransactionOrder($id);
        if ($count > 0) {
            return json(['code' => $this->f_code, 'msg' => '商品正在交易无法删除该商品']);
        }
        $shop = new ShopData();
        if ($is_shelves >= 2) {//代理商品删除
            $this->cancelGoodsProxy($id);
        }
        return json($shop->delGoods($id, $shop_id));
    }

    /**
     * 商家后端 商品列表展示
     * @param $shop_id
     * @param $page
     * @param $pre
     * @param array $sort
     * @param int $is_shelves
     * @return \think\response\Json
     */
    public function showGoodsList($shop_id, $page = 1, $pre = 15, array $sort = ['create_time' => 'desc'], $is_shelves = 1)
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->sortGoods, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $shop->showAdminGoodsList($shop_id, $page, $pre, $sort, $is_shelves),
                'total' => $shop->total(" shop_id={$shop_id} and is_shelves={$is_shelves} ", 'drh_goods')
            ]
        ]);
    }

    /**
     * 展示商户商品详情
     * @param $shop_id
     * @param $goods_id
     * @return mixed
     */
    public function showGoodsInfo($shop_id, $goods_id)
    {
        $res = Common::getTableValue('drh_goods', 'id', ['id', 'shop_id'], [$goods_id, $shop_id]);
        if (empty($res)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商品']);
        }
        $shop = new ShopData();
        $res = $shop->showAdminGoodsInfo($shop_id, $goods_id);
        unset($res['shelves_time']);
        unset($res['off_shelves_time']);
        unset($res['create_time']);
        unset($res['platform_lv']);

        $res['goods_info'] = json_decode($res['goods_info']);
        $res['goods_img'] = json_decode($res['goods_img']);
        $res['goods_attr_value'] = json_decode($res['goods_attr_value']);
        $res['goods_attr_value'] = $this->assemblyAttr($res['goods_attr_value']);
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $res
        ]);
    }

    private function assemblyAttr($goods_attr_value)
    {
        $count = count($goods_attr_value);
        $data = [];
        for ($i = 0; $i < $count; $i++) {
            $data[$i]['text'] = $goods_attr_value[$i];
            $data[$i]['focusclass'] = '';
        }
        return $data;
    }

    /**
     * 基本校验
     * @param $shop_id
     * @param $goods_id
     * @param $bargain_total_money
     * @param $bargain_once_money
     * @param $title
     * @return array|\think\response\Json
     */
    public
    function checkBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title)
    {
        $res = Common::getTableValue('drh_goods', ['goods_price', 'proxy_goods_id'], ['id', 'shop_id'], [$goods_id, $shop_id]);
        if ($res['proxy_goods_id'] != 0) {
            return ['code' => $this->f_code, 'msg' => '分销商品不能进行砍价设置'];
        }
        $goods_money = $res['goods_price'];
        if (empty($goods_money)) {
            return ['code' => $this->f_code, 'msg' => '不存在该商品'];
        }
        if ($bargain_total_money > $goods_money) {
            return ['code' => $this->f_code, 'msg' => '砍价金额不能高于商品价格'];
        }
        if (!Common::checkNumber($bargain_once_money)) {
            return ['code' => $this->f_code, 'msg' => '单次砍价金额错误'];
        }
        if (!Common::checkNumber($bargain_total_money) || !Common::checkNumber($bargain_once_money)) {
            return ['code' => $this->f_code, 'msg' => '砍价金额错误'];
        }
        $check = ['分享设置' => ["{$this->titleMax[0]},{$this->titleMax[1]}}", $title],];
        $res = Common::checkLength($check);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }

        return ['code' => $this->t_code];
    }


    /**
     * 创建砍价商品
     * @param $shop_id
     * @param $goods_id
     * @param $bargain_total_money
     * @param $bargain_once_money
     * @param $title
     * @return \think\response\Json
     */
    public
    function createBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title)
    {
        $res = Common::getTableValue('drh_goods_bargain', 'shop_id', ['shop_id', 'goods_id'], [$shop_id, $goods_id]);
        if (!empty($res)) {
            return json(['code' => $this->f_code, 'msg' => '请勿重复创建']);
        }
        $res = $this->checkBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        return json($shop->createBargainGoods($shop_id, $goods_id,
            $bargain_total_money, $bargain_once_money, $title));
    }

    /**
     * 修改砍价商品
     * @param $shop_id
     * @param $goods_id
     * @param $bargain_total_money
     * @param $bargain_once_money
     * @param $title
     * @return \think\response\Json
     */
    public
    function editBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title)
    {
        $res = $this->checkBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        return json($shop->editBargainGoods($shop_id, $goods_id, $bargain_total_money * 100,
            $bargain_once_money * 100, $title));
    }

    /**
     * 删除砍价商品
     * @param $shop_id
     * @param $goods_id
     * @return \think\response\Json
     */
    public
    function delBargainGoods($shop_id, $goods_id)
    {
        $shop = new ShopData();
        return json($shop->delBargainGoods($shop_id, $goods_id));
    }

    /**
     * 展示砍价商品列表
     * @param $shop_id
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @return array|\think\response\Json
     */
    public
    function showBargainGoods($shop_id, $page = 1, $pre = 15, array $sort = ['create_time' => 'desc'])
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->bargainSort, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFields('drh_shop', 'id', $shop_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该商户'];
        }
        $shop = new ShopData();
        $data = $shop->showBargainGoods($shop_id, $page, $pre, $sort);

        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $data,
                'total' => $shop->total('shop_id = ' . $shop_id, 'drh_goods_bargain'),
            ]
        ]);
    }

    /**
     * 展示固定砍价商品的信息
     * @param $shop_id
     * @param $goods_id
     * @return array|\think\response\Json
     */
    public
    function showBargainGood($shop_id, $goods_id)
    {
        $goods_money = Common::getTableValue('drh_goods_bargain', 'goods_id', ['goods_id', 'shop_id'], [$goods_id, $shop_id]);
        if (empty($goods_money)) {
            return ['code' => $this->f_code, 'msg' => '不存在该商品'];
        }
        $shop = new ShopData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $shop->showBargainGood($shop_id, $goods_id),
        ]);
    }

    /**
     * 添加代理商品
     * @param $goods_id
     * @param $proxy_shop_id
     * @param $shop_id
     * @param $user_id
     * @return array|\think\response\Json
     */
    public function addProxyGoods($goods_id, $proxy_shop_id, $shop_id, $user_id)
    {
        $goods_money = Common::getTableValue('drh_goods', 'id',
            ['id', 'shop_id', 'is_proxy', 'is_shelves'], [$goods_id, $proxy_shop_id, 1, 1]);
        if (empty($goods_money)) {
            return json(['code' => $this->f_code, 'msg' => '该商品无法代理']);
        }
        //校验是否重复代理
        $goods_money = Common::getTableValue('drh_goods', 'id',
            ['proxy_goods_id', 'shop_id'], [$goods_id, $shop_id]);
        if (!empty($goods_money)) {
            return json(['code' => $this->f_code, 'msg' => '请勿重复代理']);
        }
        if (empty($goods_money)) {
            return json(['code' => $this->f_code, 'msg' => '该商品无法代理']);
        }
        if (Common::checkTableFieldsValue('drh_shop', 'user_id', 'id', $shop_id, $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商户']);
        }
        $shop = new ShopData();
        $info = $shop->showAdminGoodsInfo($proxy_shop_id, $goods_id);
        return json($shop->addProxyGoods($shop_id, $info));
    }

    /**
     * 展示有代理商品商户
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @return \think\response\Json
     */
    public function showProxyShop($page = 1, $pre = 15, array $sort = ['create_time' => 'asc'])
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->bargainSort, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $shop->showProxyShop($page, $pre, $sort),
                'total' => $shop->showShopTotal()
            ]
        ]);
    }

    /**
     * 展示有代理商品商户下的代理商品
     * @param $shop_id
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @return array|\think\response\Json
     */
    public function showProxyGoods($shop_id, $page = 1, $pre = 15, array $sort = ['create_time' => 'desc'])
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->bargainSort, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFields('drh_shop', 'id', $shop_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该商户'];
        }
        $shop = new ShopData();


        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $shop->showShopProxyGoods($shop_id, $page, $pre, $sort),
                'total' => $shop->showShopProxyGoodsTotal($shop_id)
            ]
        ]);
    }

    public function showAllGoodsList($shop_id)
    {
        if (Common::checkTableFields('drh_shop', 'id', $shop_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该商户'];
        }
        $shop = new ShopData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $shop->showAllGoodsList($shop_id),
                'total' => $shop->total(" shop_id={$shop_id}", 'drh_goods')
            ]
        ]);
    }


    public function cancelGoodsProxy($goods_id)
    {
        $shop = new ShopData();
        $shop->cancelGoodsProxy($goods_id);
        return true;
    }


}